//
//  Notification+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import UIKit

extension Notification: LXSExtensionCompatible {
    
   public struct Info {
        private var userInfo: [AnyHashable: Any]?

        init(userInfo: [AnyHashable: Any]?) {
            self.userInfo = userInfo
        }

        var keyboardFrameEnd: CGRect? {
            return userInfoRect(UIResponder.keyboardFrameEndUserInfoKey)
        }

        var duration: TimeInterval? {
            return userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval
        }

        var curve: UIView.AnimationOptions? {
            return (userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt).map(UIView.AnimationOptions.init)
        }

        private func userInfoRect(_ infoKey: String) -> CGRect? {
            let frame = (userInfo?[infoKey] as? NSValue)?.cgRectValue
            if let rect = frame, rect.origin.x.isInfinite || rect.origin.y.isInfinite {
                return nil
            }
            return frame
        }
    }
}

public extension LXS where Base == Notification {
    
    var userInfo: Notification.Info {
        return Notification.Info(userInfo: base.userInfo)
    }
    
}
